package com.group.service.impl;

import com.group.mapper.GoodsMapper;
import com.group.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.entity.Good;
import pojo.entity.GoodInfoExpand;
import utils.GetGoods;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void addGood(String title, String page) {
        GetGoods getGoods = new GetGoods();
        List<Good> goodList = getGoods.getMassage(title, page);
        System.out.println(goodList);

        goodList.forEach(item -> {
            goodsMapper.addGoods(item);
            System.out.println(item);
        });
    }

    @Override
    public List<Good> findAllGoods() {
        List<Good> allGoods = goodsMapper.selectAllGoods();
        return allGoods;
    }

    @Override
    public List<Good> searchGoods(String goodName) {
        List<Good> goodList = goodsMapper.selectByName(goodName);
        return goodList;
    }

    @Override
    public Good findGoodInfoById(Long goodId) {
        return goodsMapper.selectById(goodId);
    }

    @Override
    public List<GoodInfoExpand> findGoodInfoExpand(Long goodId) {
        return goodsMapper.selectGoodsInfoExpand(goodId);
    }

    @Override
    public Integer findGoodsStarScore(Long goodId) {
        return goodsMapper.selectGoodStarScore(goodId);
    }

    @Override
    public Integer findGoodDayDeal(long goodId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();

        c.setTime(new Date());
        Date end = c.getTime();
        String today = format.format(end);//当前日期

        c.add(Calendar.DATE, -1);
        Date start = c.getTime();
        String beforeDay = format.format(start);//前一天

        System.out.println(today);
        System.out.println(beforeDay);
        return goodsMapper.selectGoodDayDeal(goodId, beforeDay, today);
    }
}
